package com.example.safeassistan.message.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseVPFragment;
import com.example.safeassistan.message.activity.Activity_Message_Detail;
import com.example.safeassistan.message.adapter.MessageAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;


/***我的消息**/
public class MessageFragment extends BaseVPFragment implements View.OnClickListener{
    private static MessageAdapter adapter;
    private RecyclerView rcView;
    private View view;
    private Context context;
    private List<Integer> data;
    private int currentPage=1;
    private int pageSize=10;
    private SmartRefreshLayout refresh;
    private int count=0;
    private int type=0;
    private LinearLayout llNodata;


    private String keys="";



    public static MessageFragment newInstance(int type ) {
        Bundle arguments = new Bundle();
        arguments.putInt("type",type);
        MessageFragment fl = new MessageFragment();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_message, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }


    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    @Override
    protected void onFragmentFirstVisible() {
            getData();
    }

    //获取数据
    public void getData(){
//        Map<String, String> map = new HashMap<>();
//        map.put("userId",getUserId(context));
//        map.put("pageNum", String.valueOf(currentPage));
//        map.put("pageSize",String.valueOf(pageSize));
//        map.put("type",String.valueOf(type));
//        map.put("keys",keys);
//        new HttpRequest(context).doPost(UrlConstants.userMessage_list, null, map, new HttpStringCallBack() {
//            @RequiresApi(api = Build.VERSION_CODES.M)
//            @Override
//            public void onSuccess(Object result) {
//                String datas = result.toString();
//                JSONObject jsonObject = null;
//                try {
//                    jsonObject = new JSONObject(datas);
//                    String message = jsonObject.getString("message");
//                    int code = jsonObject.getInt("code");
//                    if (code == 200) {
//                        Object obj = JsonUtils.fromJson(result.toString(), MessageModel.class);
//                        if (obj instanceof MessageModel) {
//                            MessageModel model=(MessageModel) obj;
//                            if (currentPage == 1) {
//                                data.clear();
//                                count = model.getCount();
//                                if (model.getData().size() > 0) {


                                    for(int i=0;i<4;i++){
                                        data.add(i);
                                    }

                                    llNodata.setVisibility(View.GONE);
                                    rcView.setVisibility(View.VISIBLE);
                                   // data=model.getData();
                                    adapter=new MessageAdapter(data);
                                    rcView.setLayoutManager(new LinearLayoutManager(context));
                                    rcView.setAdapter(adapter);
                                    if (adapter.getItemCount() < count) {
                                        //还有多的数据
                                        refresh.finishRefresh();
                                    } else {
                                        //没有更多数据（上拉加载功能将显示没有更多数据）
                                        refresh.finishRefreshWithNoMoreData();
                                    }
                                    adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                        @Override
                                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                            Activity_Message_Detail.actionStart(context,data.get(position));
                                        }
                                    });
//                                } else {
//                                    rcView.setVisibility(View.GONE);
//                                    refresh.finishRefreshWithNoMoreData();
//                                    llNodata.setVisibility(View.VISIBLE);
//                                }
//                            } else {
//                                data.addAll(model.getData());
//                                adapter.notifyDataSetChanged();
//                                if (adapter.getItemCount() < count) {
//                                    //还有多的数据
//                                    refresh.finishLoadMore();
//                                } else {
//                                    //没有更多数据（上拉加载功能将显示没有更多数据）
//                                    refresh.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
//                                }
//                            }
//                        }
//                    }else {
//                        Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
//                    }
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
//            }
//
//            @Override
//            public void onFailure(int code, String msg) {
//                refresh.finishLoadMore();
//                refresh.finishRefresh();
//            }
//        });
    }


    public void initView(){
        type=getArguments().getInt("type",0);
        refresh=view.findViewById(R.id.refresh);
        rcView=view.findViewById(R.id.rcView);
        llNodata=view.findViewById(R.id.llNodata);
        setView();
    }


    public void setView(){
        data=new ArrayList<>();
        refresh.setEnableAutoLoadMore(true);//开启自动加载功能（非必须）
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        currentPage = 1;
                        getData();
                    }
                }, 1000);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        currentPage++;
                        getData();
                    }
                }, 1000);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            default:
                break;
        }
    }
}
